package com.sgzn.sgpay.util;

import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

import java.util.Date;

/**
 * 日期工具类
 * @author zhongzhongyong
 * @version Id: DateUtil.java, v 0.1 2018/4/9 22:42 zhongzhongyong Exp $$
 */
public class DateUtil {
    public final static String TIME_FORMAT = "yyyyMMddHHmmss";
    public final static String TIME_FORMAT_2 = "yyyy-MM-dd HH:mm:ss";
    public final static String DATE_FORMAT = "yyyyMMdd";
    public final static String DATE_FORMAT_PFS = "yyyy-MM-dd";
    public final static String MONTH_FORMAT = "yyyyMM";
    public final static String DATE_FORMAT_YYMMDD = "yyyyMMdd";

    public static Date parseYYYYMMddHHMMSS(String dateStr) {
        return DateTimeFormat.forPattern(TIME_FORMAT).parseDateTime(dateStr).toDate();
    }


    public static Date parseTimeFormat(String dateStr){
        return DateTimeFormat.forPattern(TIME_FORMAT_2).parseDateTime(dateStr).toDate();
    }

    public static String getDateYYYYMMddHHMMSS() {
        return DateTimeFormat.forPattern(TIME_FORMAT).print(new DateTime());
    }

    public static String getDateYYYYMMddHHMMSS(Date date) {
        return DateTimeFormat.forPattern(TIME_FORMAT).print(new DateTime(date));
    }

    public static String getTimeStamp() {
        return DateTimeFormat.forPattern(TIME_FORMAT_2).print(new DateTime());
    }

    public static String getTimeStamp(Date date) {
        return DateTimeFormat.forPattern(TIME_FORMAT_2).print(new DateTime(date));
    }

    public static String getHHMMSS() {
        String time = DateTimeFormat.forPattern(TIME_FORMAT).print(new DateTime());
        return time.substring(8, 14);
    }

    public static String getHHMMSS(Date data) {
        String time = DateTimeFormat.forPattern(TIME_FORMAT).print(data.getTime());
        return time.substring(8, 14);
    }

    public static String getDateYYYYMMdd() {
        return DateTimeFormat.forPattern(DATE_FORMAT).print(new DateTime());
    }

    public static String getDateYYYYMMdd(Date date) {
        return DateTimeFormat.forPattern(DATE_FORMAT).print(date.getTime());
    }

    public static String getPfsDateYYYYMMdd(Date date) {
        return DateTimeFormat.forPattern(DATE_FORMAT_PFS).print(date.getTime());
    }

    public static Date getNextMinutes(Date date, int minute) {
        return new DateTime(date).plusMinutes(minute).toDate();
    }

    //获取当前月份
    public static int getCurrentMonth() {
        return DateTime.now().getMonthOfYear();
    }

    //获取上个月份
    public static int getPreviousMonth() {
        return DateTime.now().minusMonths(1).getMonthOfYear();
    }

    public static String getYYYYMM() {
        return DateTimeFormat.forPattern(MONTH_FORMAT).print(new DateTime());
    }

    /**
     * 格式化日期
     * @param date
     * @param pattern
     * @return
     */
    public static String formatDate(Date date, String pattern) {
        final DateTimeFormatter formatter = DateTimeFormat.forPattern(pattern);
        return formatter.print(new DateTime(date.getTime()));
    }

}
